'use strict';
/*BDNPLocation Controllers */
//车辆定位
app.controller('BDNPLocation', ['$scope', "locationService", '$stateParams',
    function ($scope, locationService, $stateParams) {
        $scope.location_button = "定位";

        //定位API接口必要参数
        var carid = $scope.carid = $stateParams.id;

        //页面各项初始化
        var CarNo = $scope.CarNo = $stateParams.carcode;
        //var carCode = $scope.carCode = '苏NM5052';
        var isdisabled = $scope.isdisabled2 = false;
        var creationTime = $scope.creationTime = "";
        var statusCode = $scope.statusCode = "";
        var area = $scope.area = "";
        $scope.phoneNumber = $stateParams.phone;
        $scope.areaS = "";
        $scope.creationTimeS = "";
        //起始点名称数组
        var shipAddress = $scope.shipAddress = $stateParams.shipAddress;
        var deliverAddress = $scope.deliverAddress = $stateParams.deliverAddress;
        var adds = [shipAddress, deliverAddress];

        //创建和初始化地图函数：
        function initMap() {
            createMap(); //创建地图
            setMapEvent(); //设置地图事件
            addMapControl(); //向地图添加控件
        }

        //创建地图函数：
        function createMap() {
            var map = new BMap.Map("dituContent"); //在百度地图容器中创建一个地图
            var point = new BMap.Point(119.446448, 32.164418); //定义一个中心点坐标
            map.centerAndZoom(point, 7); //设定地图的中心点和坐标并将地图显示在地图容器中
            window.map = map; //将map变量存储在全局
        }

        //地图事件设置函数：
        function setMapEvent() {
            map.enableScrollWheelZoom(); //启用地图滚轮放大缩小
        }

        //地图控件添加函数：
        function addMapControl() {
            //向地图中添加缩放控件
            var ctrl_nav = new BMap.NavigationControl({
                anchor: BMAP_ANCHOR_TOP_LEFT,
                type: BMAP_NAVIGATION_CONTROL_LARGE
            });
            map.addControl(ctrl_nav);
            //向地图中添加比例尺控件
            var ctrl_sca = new BMap.ScaleControl({
                anchor: BMAP_ANCHOR_BOTTOM_LEFT
            });
            map.addControl(ctrl_sca);
        }

        //创建InfoWindow
        //function createInfoWindow(i) {
        //    var json = markerArr[i];
        //    var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>" + json.content + "</div>");
        //    return iw;
        //}



        // 编写创建标注函数
        function addMarker(point, label) {
            var marker = new BMap.Marker(point);
            map.addOverlay(marker);
            marker.setLabel(label);

        }
        
        // 编写创建标注函数-当前定位点
        function addMarker2(point, label) {
           
            var myIcon = new BMap.Icon("/img/hc.png", new BMap.Size(48, 28));
            var marker2 = new BMap.Marker(point, { icon: myIcon });  // 创建标注
            map.addOverlay(marker2);              // 将标注添加到地图中
            marker2.setLabel(label);
            //marker2.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
            map.centerAndZoom(point, 11);
        }

        ////逆地址解析函数,起始点使用
        function s_e(adds) {
            myGeo.getPoint(adds, function (point) {
                if (point) {
                    var address = new BMap.Point(point.lng, point.lat);
                    addMarker(address, new BMap.Label(adds, {
                        offset: new BMap.Size(20, -10)
                    }));
                }
            });
        }

        //function s_e(adds) {////修改图标起始终止
        //    myGeo.getPoint(adds, function (point) {
        //        if (point) {
        //            var address = new BMap.DrivingRoute(map, { renderOptions: { map: map, autoViewport: true } });
        //            addMarker(address, new BMap.Label(adds, {
        //                offset: new BMap.Size(20, -10)
        //            }));
        //            address.search(point, point.lat, { waypoints: ["中华民族园", "对外经贸大学"] });//waypoints表示途经点
        //        }
        //    });
           
        //}
      
        Date.prototype.toLocaleString = function () {//日期时间格式2016/11/10/ 15:23:40
            return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + "/ " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
        };
        //unix时间戳转化为常用时间
        function normaltime(timer) {
            return new Date(timer - 1000).toLocaleString();
        }

        //点击定位事件
        $scope.getBDNPLocation = function () {
            $scope.location_button = "定位中，请稍等...";
            $scope.isdisabled2 = true;
            $scope.creationTime = "";
            $scope.statusCode = "";
            $scope.area = "";
            initMap();
            for (var i = 0; i < adds.length; ++i) {
                s_e(adds[i]);
            }
            
            locationService.GetBDNPLocation(CarNo).then(
                function (resq) {
                    //console.log(resq);
                    $scope.area = resq.adr;
                    if (resq.utc != null) {
                        $scope.creationTime = normaltime(resq.utc);
                    } else {
                        $scope.creationTime = new Date().getTime();
                    }
                    
                    $scope.statusCode = "定位成功";
                    $scope.isdisabled2 = false;
                    // 百度API
                    var p0 = resq.lon / 600000;//经度
                    var p1 = resq.lat / 600000;//纬度
                    var pt = new BMap.Point(p0, p1);
                    addMarker2(pt, new BMap.Label($scope.area, { offset: new BMap.Size(39, -19) }));
                    
                    $scope.location_button = "定位";
                   
                },

                function (resl) {
                    //console.log(resl);
                    $scope.statusCode = "定位失败";
                    $scope.location_button = "定位";
                    
                    $scope.isdisabled2 = true;
                }
            );
           
        }


        //返回按钮
        $scope.goback = function () {
            window.history.go(-1);
        }

        //页面进入时,地图初始化,逆地址解析实例
        initMap();
        var myGeo = new BMap.Geocoder();

      
    }
]);
